package org.javaforever.cookieshop.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.javaforever.cookieshop.domain.GoodsBean;
import org.javaforever.cookieshop.domain.RecommendBean;
import org.javaforever.cookieshop.service.GoodsService;
import org.javaforever.cookieshop.service.RecommendService;
import org.javaforever.cookieshop.utils.BooleanUtil;
import org.javaforever.cookieshop.utils.PDFUtil;
import org.javaforever.cookieshop.utils.POIExcelUtil;
import org.javaforever.cookieshop.utils.POIPPTUtil;
import org.javaforever.cookieshop.utils.StringUtil;
import org.javaforever.cookieshop.utils.WordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping(value = "/recommendController")
@RestController
public class RecommendController {
	public RecommendBean tempRecommend = new RecommendBean();
	@Autowired
	protected GoodsService goodsService;
	@Autowired
	protected RecommendService service;

	@RequestMapping(value = "/activateAllRecommends", method = RequestMethod.POST)
	public Map<String,Object> activateAllRecommends(@RequestParam(value = "ids", required = true) String ids) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.activateAllRecommends(ids);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/activateRecommend", method = RequestMethod.POST)
	public Map<String,Object> activateRecommend(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.activateRecommend(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/addRecommend", method = RequestMethod.POST)
	public Map<String,Object> addRecommend(@RequestBody RecommendBean recommend) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.addRecommend(recommend);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/checkAccessRecommend", method = RequestMethod.GET)
	public Map<String,Object> checkAccessRecommend() throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/cloneAllRecommends", method = RequestMethod.POST)
	public Map<String,Object> cloneAllRecommends(@RequestParam(value = "ids", required = true) String ids) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.cloneAllRecommends(ids);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/cloneRecommend", method = RequestMethod.POST)
	public Map<String,Object> cloneRecommend(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.cloneRecommend(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/deleteAllRecommends", method = RequestMethod.POST)
	public Map<String,Object> deleteAllRecommends(@RequestParam(value = "ids", required = true) String ids) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.deleteAllRecommends(ids);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/deleteRecommend", method = RequestMethod.POST)
	public Map<String,Object> deleteRecommend(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.deleteRecommend(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/exportPPTRecommends")
	public void exportPPTRecommends(HttpSession session,HttpServletResponse response,HttpServletRequest request) throws Exception{
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Recommends.pptx");
            List<RecommendBean> list = service.listAllRecommends();
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "推荐信息表";
            String [] headers = {"序号","名字","活跃","类型","货品"};
            Boolean [] isImages = {false,false,false,false,false};
            
            for (RecommendBean recommend:list) {
                Long goods = recommend.getGoods();
                GoodsBean goodsType0;
                String goodsName0 = "";
                if (goods != null) {
                    goodsType0 = goodsService.findGoodsById(goods);
                    if (goodsType0 !=null) goodsName0 = goodsType0.getName();
                }
                Object [] row = {StringUtil.toNullString(recommend.getId()),recommend.getName(),StringUtil.toNullString(recommend.getActive()),StringUtil.toNullString(recommend.getType()),goodsName0};
                contents.add(Arrays.asList(row));
            }
            
            POIPPTUtil.exportPPTWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/exportRecommends")
	public void exportRecommends(HttpSession session,HttpServletResponse response,HttpServletRequest request) throws Exception{
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Recommends.xls");
            List<RecommendBean> list = service.listAllRecommends();
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "推荐信息表";
            String [] headers = {"序号","名字","活跃","类型","货品"};
            Boolean [] isImages = {false,false,false,false,false};
            
            for (RecommendBean recommend:list) {
                Long goods = recommend.getGoods();
                GoodsBean goodsType0;
                String goodsName0 = "";
                if (goods != null) {
                    goodsType0 = goodsService.findGoodsById(goods);
                    if (goodsType0 !=null) goodsName0 = goodsType0.getName();
                }
                Object [] row = {StringUtil.toNullString(recommend.getId()),recommend.getName(),StringUtil.toNullString(recommend.getActive()),StringUtil.toNullString(recommend.getType()),goodsName0};
                contents.add(Arrays.asList(row));
            }
            
            POIExcelUtil.exportExcelWorkbookWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/exportRecommendsPDF")
	public void exportRecommendsPDF(HttpSession session,HttpServletResponse response,HttpServletRequest request) throws Exception{
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Recommends.pdf");
            List<RecommendBean> list = service.listAllRecommends();
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "推荐信息表";
            String [] headers = {"序号","名字","活跃","类型","货品"};
            Boolean [] isImages = {false,false,false,false,false};
            
            for (RecommendBean recommend:list) {
                Long goods = recommend.getGoods();
                GoodsBean goodsType0;
                String goodsName0 = "";
                if (goods != null) {
                    goodsType0 = goodsService.findGoodsById(goods);
                    if (goodsType0 !=null) goodsName0 = goodsType0.getName();
                }
                Object [] row = {StringUtil.toNullString(recommend.getId()),recommend.getName(),StringUtil.toNullString(recommend.getActive()),StringUtil.toNullString(recommend.getType()),goodsName0};
                contents.add(Arrays.asList(row));
            }
            
            PDFUtil.exportPDFWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/exportRecommendsWord")
	public void exportRecommendsWord(HttpSession session,HttpServletResponse response,HttpServletRequest request) throws Exception{
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Recommends.doc");
            List<RecommendBean> list = service.listAllRecommends();
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "推荐信息表";
            String [] headers = {"序号","名字","活跃","类型","货品"};
            Boolean [] isImages = {false,false,false,false,false};
            
            for (RecommendBean recommend:list) {
                Long goods = recommend.getGoods();
                GoodsBean goodsType0;
                String goodsName0 = "";
                if (goods != null) {
                    goodsType0 = goodsService.findGoodsById(goods);
                    if (goodsType0 !=null) goodsName0 = goodsType0.getName();
                }
                Object [] row = {StringUtil.toNullString(recommend.getId()),recommend.getName(),StringUtil.toNullString(recommend.getActive()),StringUtil.toNullString(recommend.getType()),goodsName0};
                contents.add(Arrays.asList(row));
            }
            
            WordUtil.exportWordWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/filterPPTRecommends", method = RequestMethod.GET)
	public void filterPPTRecommends(HttpSession session,
		HttpServletResponse response,
		HttpServletRequest request,
		@RequestParam(value = "name", required = false) String name,
		@RequestParam(value = "active", required = false) String active,
		@RequestParam(value = "type", required = false) Integer type,
		@RequestParam(value = "goods", required = false) Long goods) throws Exception{
        RecommendBean recommend0 = new RecommendBean();
        if (!StringUtil.isBlank(name)) recommend0.setName(name);
        Boolean activeBool = BooleanUtil.parseBoolean(active);
        if (activeBool != null) recommend0.setActive(activeBool);
        if (type != null) recommend0.setType(type);
        if (goods != null) recommend0.setGoods(goods);
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Recommends.pptx");
            List<RecommendBean> list = service.searchRecommendsByFields(recommend0);
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "推荐信息表";
            String [] headers = {"序号","名字","活跃","类型","货品"};
            Boolean [] isImages = {false,false,false,false,false};
            
            for (RecommendBean recommend:list) {
                Long goods0 = recommend.getGoods();
                GoodsBean goodsType0;
                String goodsName0 = "";
                if (goods0 != null) {
                    goodsType0 = goodsService.findGoodsById(goods0);
                    if (goodsType0 !=null) goodsName0 = goodsType0.getName();
                }
                Object [] row = {StringUtil.toNullString(recommend.getId()),recommend.getName(),StringUtil.toNullString(recommend.getActive()),StringUtil.toNullString(recommend.getType()),goodsName0};
                contents.add(Arrays.asList(row));
            }
            
            POIPPTUtil.exportPPTWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/filterRecommendsExcel", method = RequestMethod.GET)
	public void filterRecommendsExcel(HttpSession session,
		HttpServletResponse response,
		HttpServletRequest request,
		@RequestParam(value = "name", required = false) String name,
		@RequestParam(value = "active", required = false) String active,
		@RequestParam(value = "type", required = false) Integer type,
		@RequestParam(value = "goods", required = false) Long goods) throws Exception{
        RecommendBean recommend0 = new RecommendBean();
        if (!StringUtil.isBlank(name)) recommend0.setName(name);
        Boolean activeBool = BooleanUtil.parseBoolean(active);
        if (activeBool != null) recommend0.setActive(activeBool);
        if (type != null) recommend0.setType(type);
        if (goods != null) recommend0.setGoods(goods);
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Recommends.xls");
            List<RecommendBean> list = service.searchRecommendsByFields(recommend0);
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "推荐信息表";
            String [] headers = {"序号","名字","活跃","类型","货品"};
            Boolean [] isImages = {false,false,false,false,false};
            
            for (RecommendBean recommend:list) {
                Long goods0 = recommend.getGoods();
                GoodsBean goodsType0;
                String goodsName0 = "";
                if (goods0 != null) {
                    goodsType0 = goodsService.findGoodsById(goods0);
                    if (goodsType0 !=null) goodsName0 = goodsType0.getName();
                }
                Object [] row = {StringUtil.toNullString(recommend.getId()),recommend.getName(),StringUtil.toNullString(recommend.getActive()),StringUtil.toNullString(recommend.getType()),goodsName0};
                contents.add(Arrays.asList(row));
            }
            
            POIExcelUtil.exportExcelWorkbookWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/filterRecommendsPDF", method = RequestMethod.GET)
	public void filterRecommendsPDF(HttpSession session,
		HttpServletResponse response,
		HttpServletRequest request,
		@RequestParam(value = "name", required = false) String name,
		@RequestParam(value = "active", required = false) String active,
		@RequestParam(value = "type", required = false) Integer type,
		@RequestParam(value = "goods", required = false) Long goods) throws Exception{
        RecommendBean recommend0 = new RecommendBean();
        if (!StringUtil.isBlank(name)) recommend0.setName(name);
        Boolean activeBool = BooleanUtil.parseBoolean(active);
        if (activeBool != null) recommend0.setActive(activeBool);
        if (type != null) recommend0.setType(type);
        if (goods != null) recommend0.setGoods(goods);
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Recommends.pdf");
            List<RecommendBean> list = service.searchRecommendsByFields(recommend0);
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "推荐信息表";
            String [] headers = {"序号","名字","活跃","类型","货品"};
            Boolean [] isImages = {false,false,false,false,false};
            
            for (RecommendBean recommend:list) {
                Long goods0 = recommend.getGoods();
                GoodsBean goodsType0;
                String goodsName0 = "";
                if (goods0 != null) {
                    goodsType0 = goodsService.findGoodsById(goods0);
                    if (goodsType0 !=null) goodsName0 = goodsType0.getName();
                }
                Object [] row = {StringUtil.toNullString(recommend.getId()),recommend.getName(),StringUtil.toNullString(recommend.getActive()),StringUtil.toNullString(recommend.getType()),goodsName0};
                contents.add(Arrays.asList(row));
            }
            
            PDFUtil.exportPDFWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/filterRecommendsWord", method = RequestMethod.GET)
	public void filterRecommendsWord(HttpSession session,
		HttpServletResponse response,
		HttpServletRequest request,
		@RequestParam(value = "name", required = false) String name,
		@RequestParam(value = "active", required = false) String active,
		@RequestParam(value = "type", required = false) Integer type,
		@RequestParam(value = "goods", required = false) Long goods) throws Exception{
        RecommendBean recommend0 = new RecommendBean();
        if (!StringUtil.isBlank(name)) recommend0.setName(name);
        Boolean activeBool = BooleanUtil.parseBoolean(active);
        if (activeBool != null) recommend0.setActive(activeBool);
        if (type != null) recommend0.setType(type);
        if (goods != null) recommend0.setGoods(goods);
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Recommends.doc");
            List<RecommendBean> list = service.searchRecommendsByFields(recommend0);
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "推荐信息表";
            String [] headers = {"序号","名字","活跃","类型","货品"};
            Boolean [] isImages = {false,false,false,false,false};
            
            for (RecommendBean recommend:list) {
                Long goods0 = recommend.getGoods();
                GoodsBean goodsType0;
                String goodsName0 = "";
                if (goods0 != null) {
                    goodsType0 = goodsService.findGoodsById(goods0);
                    if (goodsType0 !=null) goodsName0 = goodsType0.getName();
                }
                Object [] row = {StringUtil.toNullString(recommend.getId()),recommend.getName(),StringUtil.toNullString(recommend.getActive()),StringUtil.toNullString(recommend.getType()),goodsName0};
                contents.add(Arrays.asList(row));
            }
            
            WordUtil.exportWordWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/findRecommendById", method = RequestMethod.POST)
	public Map<String,Object> findRecommendById(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        RecommendBean recommend = service.findRecommendById(id);
        result.put("success",true);
        result.put("data",recommend);
        return result;
	}
	@RequestMapping(value = "/findRecommendByName", method = RequestMethod.POST)
	public Map<String,Object> findRecommendByName(@RequestParam(value = "name", required = true) String name) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        RecommendBean recommend = service.findRecommendByName(name);
        result.put("success",true);
        result.put("data",recommend);
        return result;
	}
	@RequestMapping(value = "/listActiveRecommends", method = RequestMethod.POST)
	public Map<String,Object> listActiveRecommends() throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        List<RecommendBean> list = service.listActiveRecommends();
        result.put("success",true);
        result.put("rows",list);
        return result;
	}
	@RequestMapping(value = "/listAllRecommends", method = RequestMethod.POST)
	public Map<String,Object> listAllRecommends() throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        List<RecommendBean> list = service.listAllRecommends();
        result.put("success",true);
        result.put("rows",list);
        return result;
	}
	@RequestMapping(value = "/searchRecommendsByFieldsByPage", method = RequestMethod.POST)
	public Map<String,Object> searchRecommendsByFieldsByPage(@RequestParam(value = "page", required = false) Integer pagenum,
		@RequestParam(value = "rows", required = false) Integer pagesize,
		@RequestParam(value = "last", required = false) String lastFlag,
		@RequestParam(value = "name", required = false) String name,
		@RequestParam(value = "active", required = false) String active,
		@RequestParam(value = "type", required = false) Integer type,
		@RequestParam(value = "goods", required = false) Long goods) throws Exception{
        RecommendBean recommend = new RecommendBean();
        if (!StringUtil.isBlank(name)) recommend.setName(name);
        Boolean activeBool = BooleanUtil.parseBoolean(active);
        if (activeBool != null) recommend.setActive(activeBool);
        if (type != null) recommend.setType(type);
        if (goods != null) recommend.setGoods(goods);
        if (pagesize==null || pagesize <= 0) pagesize = 10;
        Integer recordCount = service.countSearchRecommendsByFieldsRecords(recommend);
        Integer pageCount = (int)Math.ceil((double)recordCount/pagesize);
        if (pageCount <= 1) pageCount = 1;
        if (pagenum==null || pagenum <= 1) pagenum = 1;
        if (pagenum >= pageCount) pagenum = pageCount;
        Boolean lastFlagBool = BooleanUtil.parseBoolean(lastFlag);
        if (lastFlagBool == null) lastFlagBool = false;
        if (lastFlagBool) pagenum = pageCount;
        Map<String,Object> result = new TreeMap<String,Object>();
        List<RecommendBean> recommendList = service.searchRecommendsByFieldsByPage(recommend,pagesize,pagenum);
        result.put("success",true);
        result.put("rows",recommendList);
        result.put("total",recordCount);
        result.put("page",pagenum);
        return result;
	}
	@RequestMapping(value = "/softDeleteAllRecommends", method = RequestMethod.POST)
	public Map<String,Object> softDeleteAllRecommends(@RequestParam(value = "ids", required = true) String ids) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.softDeleteAllRecommends(ids);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/softDeleteRecommend", method = RequestMethod.POST)
	public Map<String,Object> softDeleteRecommend(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.softDeleteRecommend(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/toggleOneRecommend", method = RequestMethod.POST)
	public Map<String,Object> toggleOneRecommend(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.toggleOneRecommend(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/toggleRecommend", method = RequestMethod.POST)
	public Map<String,Object> toggleRecommend(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.toggleRecommend(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/updateRecommend", method = RequestMethod.POST)
	public Map<String,Object> updateRecommend(@RequestBody RecommendBean recommend) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.updateRecommend(recommend);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
}

